package packaged.storage;

import play.libs.Time;
import plugin.redis.RedisDataUtils;

/**
 * 使用Redis存储数据
 */
public class RedisStorage extends Storage {
    @Override
    void add(String key, String val) {
        RedisDataUtils.set(key, val);
    }

    @Override
    void set(String key, String val) {
        set(key, val, getOvertime());
    }

    @Override
    void set(String key, String val, String overtime) {
        set(key, val, Time.parseDuration(overtime));
    }

    @Override
    void set(String key, String val, int overtime) {
        RedisDataUtils.set(key, val);
        exp(key, overtime);
    }

    @Override
    String get(String key) {
        return get(key, null);
    }

    @Override
    String get(String key, String spare) {
        String cache = RedisDataUtils.get(key);
        if (cache == null)
            return spare;
        return cache;
    }

    @Override
    void del(String key) {
        RedisDataUtils.del(key);
    }

    @Override
    String pop(String key) {
        String cache = get(key);
        del(key);
        return cache;
    }

    @Override
    void exp(String key) {
        exp(key,getOvertime());
    }

    @Override
    void exp(String key, String overtime) {
        exp(key,Time.parseDuration(overtime));
    }

    @Override
    void exp(String key, int overtime) {
        RedisDataUtils.expire(key, overtime);
    }
}
